package io.karim.materialtabs.sample;

import io.karim.MaterialRippleLayout;
import io.karim.materialtabs.sample.dialog.ConstandCodes;
import io.karim.materialtabs.sample.dialog.CustomDialog;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * RippleSettingsFragment
 *
 * @author hal
 * @since 2021/7/16
 */
public class RippleSettingsFragment extends StackLayout implements Component.ClickedListener, ResettableFragment {
    public static final String RIPPLE_DURATION = "RIPPLE_DURATION";
    public static final String RIPPLE_ALPHA_FLOAT = "RIPPLE_ALPHA_FLOAT";
    public static final String RIPPLE_COLOR = "RIPPLE_COLOR";
    public static final String RIPPLE_DELAY_CLICK = "RIPPLE_DELAY_CLICK";
    public static final String RIPPLE_DIAMETER = "RIPPLE_DIAMETER";
    public static final String RIPPLE_FADE_DURATION = "RIPPLE_FADE_DURATION";
    public static final String RIPPLE_HIGHLIGHT_COLOR = "RIPPLE_HIGHLIGHT_COLOR";
    public static final String RIPPLE_OVERLAY = "RIPPLE_OVERLAY";
    public static final String RIPPLE_PERSISTENT = "RIPPLE_PERSISTENT";
    public static final String RIPPLE_ROUNDED_CORNERS_RADIUS = "RIPPLE_ROUNDED_CORNERS_RADIUS";

    private static final int RIPPLE_DURATION_MULTIPLIER = 50;

    int rippleDurationMs;
    float rippleAlphaFloat;
    int rippleFadeDurationMs;
    int rippleRoundedCornersRadiusDp;
    float rippleDiameterDp;

    // Ripple Duration
    Slider rippleDurationSeekBar;
    Text rippleDurationTextView;

    // Ripple Alpha Float
    Slider rippleAlphaFloatSeekBar;
    Text rippleAlphaFloatTextView;

    // Ripple Color
    RadioContainer rippleColorRadioGroup;
    RadioButton rippleColorButtonWhite;

    // Ripple Delay Click
    Checkbox rippleDelayClickCheckBox;

    // Ripple Diameter
    Slider rippleDiameterSeekBar;
    Text rippleDiameterTextView;

    // Ripple Fade Duration
    Slider rippleFadeDurationSeekBar;
    Text rippleFadeDurationTextView;

    // Ripple Highlight Color
    RadioContainer rippleHighlightColorRadioGroup;
    RadioButton rippleHighlightColorButtonWhite;

    // Ripple Overlay
    Checkbox rippleOverlayCheckBox;

    // Ripple Persistent
    Checkbox ripplePersistentCheckBox;

    // Ripple Rounded Corners Radius
    Slider rippleRoundedCornersRadiusSeekBar;
    Text rippleRoundedCornersRadiusTextView;
    private MainAbility ability;

    public RippleSettingsFragment(Context context) {
        this(context, null);
    }

    public RippleSettingsFragment(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public RippleSettingsFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    private void initView() {
        Component parse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_fraction_ripple_layout,
                this, false);
        addComponent(parse);
        if (getContext() instanceof MainAbility) {
            ability = (MainAbility) getContext();
        }
        rippleDurationSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_rippleDurationSeekBar);
        rippleAlphaFloatSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_rippleAlphaFloatSeekBar);
        rippleDiameterSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_rippleDiameterSeekBar);
        rippleFadeDurationSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_rippleFadeDurationSeekBar);
        rippleDurationTextView = (Text) parse.findComponentById(ResourceTable.Id_rippleDurationTextView);
        rippleAlphaFloatTextView = (Text) parse.findComponentById(ResourceTable.Id_rippleAlphaFloatTextView);
        rippleDiameterTextView = (Text) parse.findComponentById(ResourceTable.Id_rippleDiameterTextView);
        rippleFadeDurationTextView = (Text) parse.findComponentById(ResourceTable.Id_rippleFadeDurationTextView);
        rippleColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_rippleColorRadioGroup);
        rippleColorButtonWhite = (RadioButton) parse.findComponentById(ResourceTable.Id_rippleColorButtonWhite);
        rippleDelayClickCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_rippleDelayClickCheckBox);
        rippleHighlightColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_rippleHighlightColorRadioGroup);
        rippleHighlightColorButtonWhite = (RadioButton) parse.findComponentById(ResourceTable.Id_rippleHighlightColorButtonWhite);
        rippleOverlayCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_rippleOverlayCheckBox);
        ripplePersistentCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_ripplePersistentCheckBox);
        rippleRoundedCornersRadiusSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_rippleRoundedCornersRadiusSeekBar);
        rippleRoundedCornersRadiusTextView = (Text) parse.findComponentById(ResourceTable.Id_rippleRoundedCornersRadiusTextView);
        setupAndReset();
        setClicked();
    }

    private void setClicked() {
        findComponentById(ResourceTable.Id_rippleAlphaFloatInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleDiameterInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleDurationInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleDelayClickInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleFadeDurationInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleOverlayInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_ripplePersistentInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleHighlightColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_rippleRoundedCornersRadiusInfoButton).setClickedListener(this);
    }

    @Override
    public void setupAndReset() {
        setDefault();
        setDalpha();
        setFade();
        setRounded();
        setDiameter();
        rippleDurationSeekBar.setProgressValue(rippleDurationMs / RIPPLE_DURATION_MULTIPLIER);
        rippleAlphaFloatSeekBar.setProgressValue((int) (rippleAlphaFloat * rippleAlphaFloatSeekBar.getMax()));
        rippleFadeDurationSeekBar.setProgressValue(rippleFadeDurationMs / RIPPLE_DURATION_MULTIPLIER);
        rippleRoundedCornersRadiusSeekBar.setProgressValue(rippleRoundedCornersRadiusDp);
        rippleDiameterSeekBar.setProgressValue((int) rippleDiameterDp);
        setRippleColorClicked();
        setRippleHighlightColorClicked();
        rippleColorButtonWhite.setChecked(true);
        rippleHighlightColorButtonWhite.setChecked(true);

        rippleDelayClickCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(RippleSettingsFragment.RIPPLE_DELAY_CLICK, isChecked));

        ripplePersistentCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(RippleSettingsFragment.RIPPLE_PERSISTENT, isChecked));

        rippleOverlayCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(RippleSettingsFragment.RIPPLE_OVERLAY, isChecked));

        rippleDelayClickCheckBox.setChecked(MaterialRippleLayout.DEFAULT_DELAY_CLICK);
        ripplePersistentCheckBox.setChecked(MaterialRippleLayout.DEFAULT_PERSISTENT);
        rippleOverlayCheckBox.setChecked(MaterialRippleLayout.DEFAULT_RIPPLE_OVERLAY);
    }

    private void setRippleHighlightColorClicked() {
        // Ripple Highlight Color
        rippleHighlightColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = RIPPLE_HIGHLIGHT_COLOR;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red_75);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse_75);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue_75);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange_75);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white_75);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black_75);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis_75);
                    break;
            }
        });
    }

    private void setRippleColorClicked() {
        // Ripple Color
        rippleColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = RIPPLE_COLOR;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void setDiameter() {
        rippleDiameterDp = MaterialRippleLayout.DEFAULT_DIAMETER_DP;
        rippleDiameterTextView.setText(getContext().getString(ResourceTable.String_ripple_diameter) + ": " + rippleDiameterDp + "dp");
        rippleDiameterSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                rippleDiameterDp = progress;
                rippleDiameterTextView.setText(getContext().getString(ResourceTable.String_ripple_diameter) + ": " + rippleDiameterDp + "dp");
                ability.startTabsAbilityIntent.setParam(RIPPLE_DIAMETER, rippleDiameterDp);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void setRounded() {
        rippleRoundedCornersRadiusDp = MaterialRippleLayout.DEFAULT_ROUNDED_CORNERS_DP;
        rippleRoundedCornersRadiusTextView.setText(getContext().getString(ResourceTable.String_ripple_rounded_corners_radius) + ": " + rippleRoundedCornersRadiusDp + "dp");
        rippleRoundedCornersRadiusSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                rippleRoundedCornersRadiusDp = progress;
                rippleRoundedCornersRadiusTextView.setText(
                        getContext().getString(ResourceTable.String_ripple_rounded_corners_radius) + ": " + rippleRoundedCornersRadiusDp + "dp");
                ability.startTabsAbilityIntent.setParam(RIPPLE_ROUNDED_CORNERS_RADIUS, rippleRoundedCornersRadiusDp);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void setFade() {
        rippleFadeDurationMs = MaterialRippleLayout.DEFAULT_FADE_DURATION;
        rippleFadeDurationTextView.setText(getContext().getString(ResourceTable.String_ripple_fade_duration) + ": " + rippleFadeDurationMs + "ms");
        rippleFadeDurationSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                rippleFadeDurationMs = progress * RIPPLE_DURATION_MULTIPLIER;
                rippleFadeDurationTextView.setText(getContext().getString(ResourceTable.String_ripple_fade_duration) + ": " + rippleFadeDurationMs + "ms");
                ability.startTabsAbilityIntent.setParam(RIPPLE_FADE_DURATION, rippleFadeDurationMs);
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
    }

    private void setDalpha() {
        rippleAlphaFloat = MaterialRippleLayout.DEFAULT_ALPHA;
        rippleAlphaFloatTextView.setText(getContext().getString(ResourceTable.String_ripple_alpha_float) + ": " + rippleAlphaFloat);
        rippleAlphaFloatSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                rippleAlphaFloat = (float) progress / rippleAlphaFloatSeekBar.getMax();
                rippleAlphaFloatTextView.setText(getContext().getString(ResourceTable.String_ripple_alpha_float) + ": " + rippleAlphaFloat);
                ability.startTabsAbilityIntent.setParam(RIPPLE_ALPHA_FLOAT, rippleAlphaFloat);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void setDefault() {
        rippleDurationMs = MaterialRippleLayout.DEFAULT_DURATION;
        rippleDurationTextView.setText(getContext().getString(ResourceTable.String_ripple_duration) + ": " + rippleDurationMs + "ms");
        rippleDurationSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                rippleDurationMs = progress * RIPPLE_DURATION_MULTIPLIER;
                rippleDurationTextView.setText(getContext().getString(ResourceTable.String_ripple_duration) + ": " + rippleDurationMs + "ms");
                ability.startTabsAbilityIntent.setParam(RIPPLE_DURATION, rippleDurationMs);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }

        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_rippleAlphaFloatInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_alpha_float), getContext().getString(ResourceTable.String_ripple_alpha_float_details));
                break;
            case ResourceTable.Id_rippleColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_color), getContext().getString(ResourceTable.String_ripple_color_details));
                break;
            case ResourceTable.Id_rippleDiameterInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_diameter), getContext().getString(ResourceTable.String_ripple_diameter_details));
                break;
            case ResourceTable.Id_rippleDurationInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_duration), getContext().getString(ResourceTable.String_ripple_duration_details));
                break;
            case ResourceTable.Id_rippleDelayClickInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_delay_click), getContext().getString(ResourceTable.String_ripple_delay_click_details));
                break;
            case ResourceTable.Id_rippleFadeDurationInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_fade_duration), getContext().getString(ResourceTable.String_ripple_fade_duration_details));
                break;
            case ResourceTable.Id_rippleOverlayInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_overlay), getContext().getString(ResourceTable.String_ripple_overlay_details));
                break;
            case ResourceTable.Id_ripplePersistentInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_persistent), getContext().getString(ResourceTable.String_ripple_persistent_details));
                break;
            case ResourceTable.Id_rippleHighlightColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_highlight_color), getContext().getString(ResourceTable.String_ripple_highlight_color_details));
                break;
            case ResourceTable.Id_rippleRoundedCornersRadiusInfoButton:
                showDialog(getContext().getString(ResourceTable.String_ripple_rounded_corners_radius), getContext().getString(ResourceTable.String_ripple_rounded_corners_radius_details));
                break;
            default:
                break;
        }
    }

    private void showDialog(String title, String content) {
        CustomDialog.init(getContext())
                .setLayoutId(ResourceTable.Layout_dialog_info_layout, ConstandCodes.COMMONTYPE)
                .setViewConvertListener((holder, dialog) -> {
                    holder.getView(ResourceTable.Id_yes_or_no_layout).setVisibility(Component.HIDE);
                    Text tv_title = holder.getView(ResourceTable.Id_tv_title);
                    tv_title.setText(title);
                    Text tv_content = holder.getView(ResourceTable.Id_tv_content);
                    tv_content.setText(content);
                })
                .setSwipeToClose(true)
                .setTransparent(true)
                .show();
    }
}
